/*******************************************************************************
 * Copyright (c) 2004, 2007 Mylyn project committers and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/

package org.eclipse.mylyn.rememberthemilk.core.model;

import java.util.ArrayList;
import java.util.List;

/**
 * Represents a search criterion. Each criterion is applied to a field such as milestone or priority. It has a compare
 * operator and a list of values. The compare mode is <code>OR</code> for the operators <code>contains</code>,
 * <code>starts with</code>, <code>ends with</code> and <code>is</code>. The compare mode is <code>AND</code>
 * for all other (negated) operators.
 * 
 * @author Steffen Pingel
 */
public class RTMSearchFilter {

	public enum CompareOperator {
		AND("AND"), OR("OR"), NOT("NOT"), CONTAINS(""), CONTAINS_NOT("NOT");

		/** The string that represent the operator in a Trac query. */
		private String queryValue;
		
		public static CompareOperator fromUrl(String value) {
			for (CompareOperator operator : values()) {
				if (operator != CONTAINS && operator != CONTAINS_NOT && value.startsWith(operator.queryValue)) {
					return operator;
				}
			}
			if (value.startsWith(CONTAINS.queryValue)) {
				return CONTAINS;
			}
			return OR;
		}

		CompareOperator(String queryValue) {
			this.queryValue = queryValue;
		}

		public String getQueryValue() {
			return queryValue;
		}

		@Override
		public String toString() {
			switch (this) {
				case CONTAINS:
					return "contains";
				case CONTAINS_NOT:
					return "does not contain";
				case AND:
					return "and";
				case OR:
					return "or";
				case NOT:
					return "not";
				default:
					return "and";
				}
		}
	}

	private String fieldName;

	private CompareOperator operator;

	private List<String> values = new ArrayList<String>();

	public RTMSearchFilter(String fieldName) {
		this.fieldName = fieldName;
	}

	public void addValue(String value) {
		values.add(value);
	}

	public String getFieldName() {
		return fieldName;
	}

	public CompareOperator getOperator() {
		return operator;
	}

	public List<String> getValues() {
		return values;
	}

	public void setOperator(CompareOperator operator) {
		this.operator = operator;
	}

}
